//
//  WebView.swift
//  AliBaiChuan
//
//  Created by 郑东喜 on 2017/2/20.
//  Copyright © 2017年 郑东喜. All rights reserved.
//

import UIKit



class WebView: BaseWebView {

    ///分享按钮
    fileprivate lazy var shareBtn: UIBarButtonItem = {
        let share : UIBarButtonItem = UIBarButtonItem.init(title: "分享", style: .plain, target: self, action: #selector(shareSEL))
        return share
    }()
    
    fileprivate lazy var backSEL: UIBarButtonItem = {
        let btn : UIBarButtonItem = UIBarButtonItem.init(title: "返回", style: .plain, target: self, action: #selector(leftBarSEL))
        return btn
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
       
        //分享视图
        self.navigationItem.rightBarButtonItem = shareBtn
        self.navigationItem.leftBarButtonItem = backSEL
        
        if _webArray.count > 0 {
            self.weView.loadRequest(URLRequest.init(url: URL.init(string: (_webArray.lastObject as? String)!)!))
            
        } else {
            
        }
    }
    
    //分享视图
    lazy var shreView: YSJShareView = {
        var shareView : YSJShareView = YSJShareView()
        
        shareView = YSJShareView.init(frame: CGRect(x: 0, y: UIScreen.main.bounds.height - 80, width: UIScreen.main.bounds.width, height: 80))
        shareView._delegate = self
        shareView.addItem("QQ", withImage: UIImage(named: "s_QQ")!)
        shareView.addItem("QQ空间", withImage: UIImage(named: "s_QZone")!)
        shareView.addItem("微信朋友圈", withImage: UIImage(named: "wx_Fri")!)
        shareView.addItem("微信好友", withImage: UIImage(named: "wx_Zone")!)
        shareView.layer.cornerRadius = 5
        
        return shareView
    }()
}


// MARK:- 分享
extension WebView : YSJShareViewDelegate {
    internal func shareBtnClick(_ index: Int) {
        print("\((#file as NSString).lastPathComponent):(\(#line))\n",index)
    }

    func shareSEL() -> Void {
        print("\((#file as NSString).lastPathComponent):(\(#line))\n")
        
        shreView.show()
    }
}


extension WebView {
    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        
        let urlString = request.url?.absoluteString

        //根据单机状态跳转
        if navigationType == .linkClicked || navigationType == .formResubmitted || navigationType.rawValue == 0 || navigationType == .reload {
            
            self.url = urlString!
            
            if _webArray.count > 0 && _webArray.lastObject as? String == urlString  {

                _webArray.removeLastObject()
                return true
            }

            
            _webArray.add(urlString as Any)
            
            self.jumpReVi(replaceURL: urlString!)
                        
            return false
        } else {
            return true
        }
        
        return true
    }
}

// MARK:- 跳转替身网页
extension WebView {
    
    /// 替身网页跳转
    ///
    /// - Parameter url: 替身网页跳转的url
    func jumpReVi(replaceURL url: String) -> Void {
        
        let vvv : WebReplaceV = WebReplaceV()
        vvv.url = self.url
        self.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(WebReplaceV(), animated: true)
    }
}

extension WebView {
    @objc fileprivate func leftBarSEL() -> Void {
        print("\((#file as NSString).lastPathComponent):(\(#line))\n")
        self.weView.goBack()
        
        print("\((#file as NSString).lastPathComponent):(\(#line))\n",self.weView.canGoBack)
        
        if self.weView.canGoBack == false {
            self.navigationController?.popToRootViewController(animated: true)
            self.weView.delegate = nil
            self.weView.removeFromSuperview()
        }
    }
}
